<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>教师账号管理</title>
    <link rel="stylesheet" type="text/css" href="../easyUI/themes/default/easyui.css">
    <link rel="stylesheet" type="text/css" href="../easyUI/themes/icon.css">
    <script type="text/javascript" src="../easyUI/jquery.min.js"></script>
    <script type="text/javascript" src="../easyUI/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="../addressIP.js"></script>
    <script type="text/javascript" src="../easyUI/datagrid-export.js"></script>
    <script type="text/javascript" src="../downLoadExcelModel.js"></script>
    <script type="text/javascript" src="../xlsx.full.min.js"></script>
</head>
<body>
<table id="dg"></table>
<script type="application/javascript">
    $('#dg').datagrid({
        columns: [
            [
                {
                    field: 'account',
                    title: '账号',
                    width: '25%',
                    align: 'center'
                },
                {
                    field: 'name',
                    title: '姓名',
                    width: '25%',
                    align: 'center'
                },
                {
                    field: 'password',
                    title: '密码',
                    width: '25%',
                    align: 'center'
                },
                {
                    field: 'teleNumber',
                    title: '电话',
                    width: '25%',
                    align: 'center'
                },
            ]
        ]
    });
</script>
<script type="text/javascript">
    $(function() {
        var dg = $('#dg').datagrid({
            url: addressip + "/superGetTeacher",
            queryParams: {
                superAccount: localStorage.getItem("superAccount"),
                password: localStorage.getItem("password")
            },
            fitColumns: true,
            loadMsg: '数据加载中^_^',
            striped: true, //显示斑马线
            loadMsg: '等等我。。',
            idField: 'userID',
            rownumbers: true,
            pagination: true,
            fit: true,
            fitColumns: true,
            checkbox: true,
            autoRowHeight: false,
            emptyMsg: '暂无数据',
            pageSize: 15,
            pageList: [15, 25, 50, 99999995]
        });
        <!-- 工具栏-->
        var fbutton = dg.datagrid('getPager');
        fbutton.pagination({
            buttons: [{
                iconCls: 'icon-redo',
                handler: function() {
                    $.messager.confirm("操作提示", "本操作将导出展示的内容为Excel到Windows默认下载文件夹，可能需要等待较长时间，点击确定继续。", function(data) {
                        if (data) {
                            $('#dg').datagrid('toExcel', 'C:\Users\Public\Downloads\dg.xls');
                        } else {
                            $.messager.confirm("操作提示", "已取消");
                        }
                    });
                }
            },
                {
                    iconCls: 'icon-add',
                    handler: function() {
                        $('#registerUsersByExcel').dialog('open');
                        $('#registerUsersByExcel').dialog('center');
                    }
                },
                {
                    iconCls: 'icon-edit',
                    handler: function() {
                        //$('#users').dialog('cleanSec');
                        selectedrow = $('#dg').datagrid('getSelections');

                        var le = selectedrow.length;
                        if (le == 0) {
                            $.messager.alert('提示', '请选择需要修改的数据', 'warning');
                        } else {
                            if (le != 1) {
                                var temstr = "您本次一共选择了" + le.toString() + "条数据";
                                $.messager.confirm("操作提示", temstr, function(data) {
                                    if(data){
                                        $('#dlg2').dialog('open');
                                        $('#dlg2').window('center');
                                    }
                                });
                            }else{
                                $('#dlg2').dialog('open');
                                $('#dlg2').window('center');
                            }

                        }
                    }
                },
                {
                    iconCls: 'icon-man',
                    handler: function() {
                        $('#dlgaddTeacher').dialog('open');
                        $('#dlgaddTeacher').window('center');
                    }
                },
                {
                    iconCls: 'icon-clear',
                    handler: function() {
                        $("#dg").datagrid('clearSelections');
                    }
                }
            ]
        });
    });
</script>

<div id="registerUsersByExcel" class="easyui-dialog" title="批量注册用户" data-options="iconCls:'icon-save',closed:true"
     style="width:400px;height:290px;padding:10px">
    <div>
        <script type="text/javascript">
            var jsono = [{
                "account": "",
                "name": "",
                "className": "",
                "teleNumber": ""
            }];
        </script>
        <a href="" onclick="downloadExl(jsono)" id="returnExcelModel" class="easyui-linkbutton" iconCls="icon-ok" style="width:100%;height:32px">点击下载模板Excel</a>
        <a href="" download="批量导入用户模板.xlsx" id="hf"></a>
    </div>
    <div>
        <input type="file" onchange="importf(this)" />
        <script type="text/javascript">
            var wb; //读取完成的数据
            var rABS = false; //是否将文件读取为二进制字符串

            function importf(obj) { //导入
                if (!obj.files) {
                    return;
                }
                var f = obj.files[0];
                var reader = new FileReader();
                reader.onload = function(e) {
                    var data = e.target.result;
                    if (rABS) {
                        wb = XLSX.read(btoa(fixdata(data)), { //手动转化
                            type: 'base64'
                        });
                    } else {
                        wb = XLSX.read(data, {
                            type: 'binary'
                        });
                    }
                    //wb.SheetNames[0]是获取Sheets中第一个Sheet的名字
                    //wb.Sheets[Sheet名]获取第一个Sheet的数据
                    var toWebServiceStringOfUser = JSON.stringify(XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]]));
                    var xhruser = new XMLHttpRequest();
                    var usersurl = addressip + "/superExcelRegisterTeacher";
                    xhruser.open("POST", usersurl, "true");
                    xhruser.setRequestHeader("Content-Type", "application/json;charset=utf-8");
                    xhruser.send(toWebServiceStringOfUser);
                    xhruser.onreadystatechange = function() {
                        if (xhruser.readyState == 4 && xhruser.status == 200) {
                            var finishResult = xhruser.responseText;
                            $.messager.alert("操作提示", finishResult, "warning");
                        };
                    };
                };
                if (rABS) {
                    reader.readAsArrayBuffer(f);
                } else {
                    reader.readAsBinaryString(f);
                }
            }

            function fixdata(data) { //文件流转BinaryString
                var o = "",
                    l = 0,
                    w = 10240;
                for (; l < data.byteLength / w; ++l) o += String.fromCharCode.apply(null, new Uint8Array(data.slice(l * w, l * w +
                    w)));
                o += String.fromCharCode.apply(null, new Uint8Array(data.slice(l * w)));
                return o;
            }
        </script>
    </div>
</div>
<div id="dlgaddTeacher" class="easyui-dialog" title="新增教师账号" data-options="iconCls:'icon-save',closed:true" style="width:370px;height:450px;padding:10px">
    <div style="margin-bottom:20px">
        <input id="addnumber" class="easyui-textbox" label="联系方式:" labelPosition="top" data-options="prompt:'请输入手机号'"
               style="width:100%;">
    </div>
    <div style="margin-bottom:20px">
        <input id="addpassword" class="easyui-passwordbox" label="密码:" labelPosition="top" data-options="prompt:'请输入密码'"
               style="width:100%;">
    </div>
    <div style="margin-bottom:20px">
        <input id="addpasswordTwo" class="easyui-passwordbox" label="确认密码:" labelPosition="top" data-options="prompt:'再次输入密码'"
               style="width:100%;">
    </div>
    <div style="margin-bottom:20px">
        <input id="addname" class="easyui-textbox" label="姓名:" labelPosition="top" data-options="prompt:'教师姓名'"
               style="width:100%;">
    </div>

    <div style="margin-bottom:20px">
        <a href="" id="addTeacher" class="easyui-linkbutton" iconCls="icon-ok" style="width:100%;height:32px">添加</a>
    </div>
    <script>
        $("#addTeacher").click(function() {
            var anumber = $("#addnumber").val().trim();
            var apassword = $("#addpassword").val().trim();
            var apasswordd = $("#addpasswordTwo").val().trim();
            var aname = $("#addname").val().trim();
            if(anumber.trim().length != 11){
                alert("请输入十一位手机号");
            }else {
                if(apassword != apasswordd){
                    alert("请检查密码");
                }else{
                    var sendteacher = {};
                    sendteacher.teleNumber = anumber;
                    sendteacher.account = anumber;
                    sendteacher.name = aname;
                    sendteacher.password = apassword;
                    sendteacher.className = "666";
                    var listteacher = [];
                    listteacher[0] = sendteacher;
                    var toWebServiceStringOfUser = JSON.stringify(listteacher);
                    var xhruser = new XMLHttpRequest();
                    var usersurl = addressip + "/superExcelRegisterTeacher";
                    xhruser.open("POST", usersurl, "true");
                    xhruser.setRequestHeader("Content-Type", "application/json;charset=utf-8");
                    xhruser.send(toWebServiceStringOfUser);
                    xhruser.onreadystatechange = function() {
                        if (xhruser.readyState == 4 && xhruser.status == 200) {
                            var finishResult = xhruser.responseText;
                            $.messager.alert("操作提示", finishResult, "warning");
                        };
                    };
                }
            }
        });
    </script>
</div>
<div id="dlg2" class="easyui-dialog" title="修改用户信息" data-options="iconCls:'icon-save',closed:true" style="width:400px;height:290px;padding:10px">
    <div style="margin-bottom:20px">
        <input id="changename" class="easyui-textbox" label="姓名:" labelPosition="top" data-options="prompt:'新姓名'" style="width:100%;">
    </div>
    <div style="margin-bottom:20px">
        <input id="changepassword" class="easyui-textbox" label="密码:" labelPosition="top" data-options="prompt:'请输入密码'"
               style="width:100%;">
    </div>
    <div style="margin-bottom:20px">
        <input id="changenumber" class="easyui-textbox" label="联系方式:" labelPosition="top" data-options="prompt:'联系方式(请保证选中一条数据)'"
               style="width:100%;">
    </div>
    <div>
        <a href="" id="savechange" class="easyui-linkbutton" iconCls="icon-ok" style="width:100%;height:32px">确认修改</a>
        <div id="" style="height: 20px;">

        </div>
        <script type="text/javascript">
            $("#savechange").click(function() {
                var tname = $("#changename").val();
                var tclass = $("#changeclass").val();
                var tpassword = $("#changepassword").val();
                var tnumber = $("#changenumber").val();
                var changeurl = addressip + "/superChangeUser";
                var sendaccount = {};
                sendaccount.account = localStorage.getItem("superAccount");
                sendaccount.password = localStorage.getItem("password");
                for (var i = 0; i < selectedrow.length; i++) {
                    if (tname) {
                        selectedrow[i].name = tname;
                    }
                    if (tpassword) {
                        selectedrow[i].password = tpassword;
                    }
                    if (tclass) {
                        selectedrow[i].className = tclass;
                    }
                    if (tnumber) {
                        selectedrow[i].teleNumber = tnumber;
                    }
                }
                sendaccount.storage = selectedrow;
                $.ajax({
                    //请求方式
                    type: "POST",
                    dataType: "json",
                    url: changeurl,
                    headers: {
                        'Content-Type': 'application/json;charset=utf-8'
                    },
                    data: JSON.stringify(sendaccount),
                    //请求成功
                    success: function(data) {
                        alert(data);
                    },
                    //请求失败，包含具体的错误信息
                    error: function(e) {
                        alert(e.status);
                        alert(e.responseText);
                    }
                });
            });
        </script>
    </div>
</div>
</body>
</html>